package ajakuulauku;

import java.util.Timer;
import java.util.TimerTask;

/**
 * 
 * Implements timer. Can be used by other components that need timer.
 */
public abstract class Engine extends TimerTask{
	Timer timer;
	protected GUICanvas canv = new GUICanvas();
	
	/**
	 * 
	 * Processes game logic.
	 * Must be overriden in classes extending Engine.
	 */
	abstract void update();
	
	/**
	 * 
	 * Schedules run method to be run at specified interval.
	 * @param interval specified interval
	 */
	public void schedule(int interval) {
		timer = new Timer();
		timer.scheduleAtFixedRate(this, 0, interval);
	}
	
	/**
	 * Properly ends the level and starts a new game.
	 */
	public void cleanUp() {
      timer.cancel();
      CurrentLevel.resetLevel();
      canv.gui.dispose();
      Main.startGame();
	}
	
	/**
	 * 
	 * Runs update method.
	 */
	@Override
	public void run() {
		update();
	}
}
